var $ = require("../../utils/Ajax.js");
Page({
  data: {
    // 图片路径配置
    PictureUrl: getApp().PictureUrl,
    // 商品数量
    Quantity: null,

    // 是否是编辑状态
    EditStatus: false,

    Cart: null,
    BtnBoxLeftStatus: true,

    // 合计金额
    Total: null,

    // 删除的数据下标
    DelectIndex: null,
  },

  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "购物车"
    })
    $.Ajax("api/cart","GET",{},function(res){
      if(res.s){
        var Total = 0;
        for (var i = 0; i < res.d.cart.length;i++){
          Total = Total + Number(res.d.cart[i].price) * Number(res.d.cart[i].num)
          res.d.cart[i].isCheck = true;
        }
        that.setData({
          Quantity: res.d.cart.length,
          Total: Total,
          Cart: res.d.cart
        })
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  onShow: function () {
    
  },

  // 点击管理按钮
  Edit:function(){
    this.setData({
      EditStatus: true,
      BtnBoxLeftStatus: false,
      DelectIndex: null
    })
    var Cart = this.data.Cart;
    for (var i = 0; i < Cart.length; i++) {
      var isCheck = 'Cart[' + i + '].isCheck';
      this.setData({
        [isCheck]: false
      })
    }
  },

  // 点击完成管理按钮
  EditOk:function(){
    this.setData({
      EditStatus: false,
      BtnBoxLeftStatus: true,
      DelectIndex: null
    })
    var Cart = this.data.Cart;
    for (var i = 0; i < Cart.length; i++) {
      var isCheck = 'Cart[' + i + '].isCheck';
      this.setData({
        [isCheck]: true
      })
    }
  },

  // 点击购物车商品状态
  CartListStatus: function(e){
    var Quantity = this.data.Quantity;
    var Index = e.detail.value;
    this.setData({
      DelectIndex: Index
    })
    if (e.detail.value.length == Quantity) {
      this.setData({
        BtnBoxLeftStatus: true
      })
    } else {
      this.setData({
        BtnBoxLeftStatus: false
      })
    }
  },

  // 点击全选
  CartBottomStatus: function(e){
    var that = this;
    var BtnBoxLeftStatus = e.detail.value[0];
    var Cart = that.data.Cart;
    if (BtnBoxLeftStatus) {
      var DelectIndex = [];
      for (var i = 0; i < Cart.length; i++) {
        var isCheck = 'Cart[' + i + '].isCheck';
        DelectIndex.push(String(i));
        that.setData({
          [isCheck]: true,
          BtnBoxLeftStatus: true,
          DelectIndex: DelectIndex
        })
      }
    } else {
      for (var i = 0; i < Cart.length; i++) {
        var isCheck = 'Cart[' + i + '].isCheck';
        that.setData({
          [isCheck]: false,
          BtnBoxLeftStatus: false,
          DelectIndex: null
        })
      }
    }
  },

  // 点击减少商品
  Jian: function (e) {
    var that = this;
    var Cart = this.data.Cart;
    var Index = e.currentTarget.dataset.index_;
    var Id = e.currentTarget.dataset.id;
    var Aid = e.currentTarget.dataset.aid;
    var CartNumber = e.currentTarget.dataset.num;
    if (CartNumber == 1) {
      wx.showToast({
        title: '不能再减了',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    } else {
      --CartNumber;
      $.Ajax("api/editCart", "POST", {
        id: Id,
        aid: Aid,
        num: CartNumber
      }, function (res) {
        if (res.s){
          var Num = 'Cart[' + Index + '].num';
          var Jia = Cart[Index].price;
          that.setData({
            [Num]: CartNumber,
            Total: Number(that.data.Total) - Number(Jia)
          })
        }else{
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      }) 
    }
  },

  // 点击增加商品
  Jia: function (e) {
    var that = this;
    var Cart = this.data.Cart;
    var Total = this.data.Total;
    var Index = e.currentTarget.dataset.index_;
    var Id = e.currentTarget.dataset.id;
    var Aid = e.currentTarget.dataset.aid;
    var CartNumber = e.currentTarget.dataset.num;
    ++CartNumber;
    $.Ajax("api/editCart", "POST", {
      id: Id,
      aid: Aid,
      num: CartNumber
    }, function (res) {
      if(res.s){
        var Num = 'Cart[' + Index + '].num';
        var Jia = Cart[Index].price;
        that.setData({
          [Num]: CartNumber,
          Total: Number(that.data.Total) + Number(Jia)
        })
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1000,
          mask: true
        })
      }
    })
  },

  // 删除商品
  DelectOrder: function () {
    var ids = '';
    var Total = 0;
    var Cart = this.data.Cart;
    var DelectIndex = this.data.DelectIndex.sort(function(a,b){ return a < b ? 1 : -1 });
    for (var i = 0; i < DelectIndex.length; i++) {
      ids += String(Cart[DelectIndex[i]].id) + ",";
      Cart.splice(DelectIndex[i],1);
    }
    for (var j = 0; j < Cart.length; j++){
      Total += Number(Cart[j].price) * Number(Cart[j].num) * Number(Cart[j].box)
    }
    this.setData({
      Cart: Cart,
      Quantity: Cart.length,
      Total: Total
    })
    // var Ids = ids.substr(0, ids.length - 1);  
    $.Ajax("api/delCart","POST",{
      ids: ids
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 1500,
        mask: true
      })
    })
  },

  // 点击提交订单
  SubmitOrder: function(){
    var cartId = '';
    var that = this;
    var Cart = that.data.Cart;
    for (var i = 0; i < Cart.length;i++){
      cartId += String(Cart[i].id) + ",";
    }
    $.Ajax("api/confirm","POST",{
      cartId: cartId
    },function(res){
      if(res.s){
        var OrderInfor = JSON.stringify(res.d);
        wx.removeStorageSync('SubmitOrderAddress');
        wx.navigateTo({
          url: '../SubmitOrder/SubmitOrder?OrderInfor=' + OrderInfor,
        })
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500,
          mask: true
        })
      }
    })
  }
})